package com.rtwo.controller;

import co.Apple;
import com.rtwo.entity.Department;
import com.rtwo.entity.User;
import com.rtwo.model.Address;
import com.rtwo.model.Person;
import com.rtwo.redisdao.RedisUtil;
import com.rtwo.service.DepartmentService;
import com.rtwo.service.EmailServiceImpl;
import com.rtwo.service.RoleService;
import com.rtwo.service.UserService;
import com.rtwo.test.Boot;
import com.rtwo.test.Hello;
import com.rtwo.test.Test;
import com.rtwo.utils.Coffee;
import com.rtwo.utils.MySettings;
import com.rtwo.utils.SpringUtil;
import com.sun.javafx.scene.shape.PathUtils;
import javafx.util.Pair;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
import org.apache.catalina.servlet4preview.http.HttpFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.support.ResourcePatternUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import javax.crypto.KeyGenerator;
import javax.net.ssl.HttpsURLConnection;
import javax.servlet.http.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.ResourceBundle;

/**
 * Created by ThinkPad on 2017/4/7.
 */
@Controller
public class MainController {

    @Resource
    private DepartmentService departmentService;

    @Resource
    private UserService userService;

    @Resource
    private RoleService roleService;

    @Resource
    private Boot boot;

    @Resource
    private SpringUtil springUtil;

    @Autowired
    private MySettings mySettings;


    @RequestMapping("/save")
    public String save() {
        Department dep1 = new Department();
        dep1.setName("dep1");
        departmentService.save(dep1);

        Department dep2 = new Department();
        dep2.setName("dep2");
        departmentService.save(dep2);

        Department dep3 = new Department();
        dep3.setName("dep3");
        departmentService.save(dep3);

        Department dep4 = new Department();
        dep4.setName("dep4");
        departmentService.save(dep4);

        User user1 = new User();
        user1.setName("zhouyao");
        user1.setCreatedate(new Date());
        user1.setDep(dep1);
        userService.save(user1);

        User user2 = new User();
        user2.setName("zhouyao");
        user2.setCreatedate(new Date());
        user2.setDep(dep1);
        userService.save(user2);

        User user3 = new User();
        user3.setName("zhouyao");
        user3.setCreatedate(new Date());
        user3.setDep(dep4);
        userService.save(user3);

        User user4 = new User();
        user4.setName("zhouyao");
        user4.setCreatedate(new Date());
        user4.setDep(dep4);
        userService.save(user4);

        return "ok";
    }

    @Resource
    private Coffee coffee;

    @Resource
    private Apple apple;

    @Autowired
    private EmailServiceImpl emailService;

    @Resource
    private RedisUtil redisUtil;

    @Resource
    private Hello hello;


    private static int count = 1;

    @RequestMapping(value = "/test")
    public String test(HttpServletRequest req, HttpServletResponse res) {
        Person person = new Person();
        Address address = new Address();
        address.setLanguage("cn");
        address.setLocation("china");
        person.setAddress(address);
        person.setName("zhouhao");
        person.setSex("male");
        req.getSession().setAttribute("user",person);
        req.setAttribute("str",new String[]{"1","2","3"});
        return "test";
    }

    private static int count1 = 1;
    @Autowired
    private CacheManager cacheManager;

    @RequestMapping(value = "/test1")
    public String test1(HttpServletRequest req, HttpServletResponse res){
        req.setAttribute("name","zhouhao");
        Cache rtwo = cacheManager.getCache("rtwo");
        Element rtwo1 = rtwo.get("rtwo1");
        if(rtwo1==null){
            System.out.println("没有缓存");
            rtwo.put(new Element("rtwo1","rtwo1"));
            req.setAttribute("key","没有缓存");
        }else{
            System.out.println("有缓存");
            req.setAttribute("key",rtwo1.getObjectValue().toString());
        }
        return "test";
    }

    @RequestMapping(value = "/hellos")
    public String hello(HttpServletRequest req, HttpServletResponse res) {
        return "index/hello";
    }

    @RequestMapping("/getDepNameByUserName")
    @ResponseBody
    public String getDepNameByUserName(String name) {
        return userService.findUserByName(name).getDep().getName();
    }

    @RequestMapping("/file")
    public String file() {
        return "file/upload";
    }

    @RequestMapping("/upload")
    @ResponseBody
    public String upload(MultipartFile file) {
        try {
            byte[] bytes = file.getBytes();
            FileOutputStream fileOutputStream = new FileOutputStream(new File("downloads/" + file.getOriginalFilename()));
            fileOutputStream.write(bytes);
            fileOutputStream.flush();
            fileOutputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "ok";
    }


    @RequestMapping("/index")
    public String index(){
        return "shiro/index";
    }

    @RequestMapping("/login")
    public String login(){
        return "shiro/login";
    }

    @RequestMapping("/err")
    public String _403(){
        return "shiro/403";
    }

}

